//FACEBOOK ASYNC CLIENT CLASS IMPLEMENTATION

#include "facebookasync.h"

#include "xmlParser.h"
#include "md5.h"
#include <fstream>

//Constructor and destructor
//==========================================================================

int FacebookAsync::ConstructorTimes = 0;
int FacebookAsync::DestructorTimes = 0;
FacebookAsync* FacebookAsync::m_pInst = NULL;

FacebookAsync::FacebookAsync()
{
	ConstructorTimes++;

	//main data reset
	m_AppKey = "";
	m_AppSecret = "";
	m_ApiServer = "";
	m_LoginURL = "";
	m_CallID = 0;

	//error description reset
	m_ErrorDescription = "";
	m_FacebookSiteChanged = false;
	m_InvalidLoginPassword = false;

	//users reset
	m_UsersList.clear();
	m_pCurUser = NULL;

	m_pCallbackFunc = NULL;

}//Facebook::Facebook()


FacebookAsync::~FacebookAsync()
{
	m_pCurUser = NULL;

	DestructorTimes++;

	//cleaning users list
	std::list<FacebookAsyncUser*>::iterator Iter;

	if (m_UsersList.empty() == false)
	{
		for (Iter = m_UsersList.begin(); Iter != m_UsersList.end(); Iter++)
		{
			FacebookAsyncUser* TmpUser = (*Iter);

			if (TmpUser != NULL)
			{
				//TODO LOGOUT
				//if user was logged in - then logout
				//if (TmpUser->m_LoggedIn == true)
				//	Helper_LogoutUser(TmpUser);

				//deleting user
				//delete TmpUser; TEST TO FIND OUT IF IT'S A BUG SOURCE
			}
		}
	}
}




//Init
//==========================================================================

bool FacebookAsync::Init(const std::string& AppKey, const std::string& AppSecret, const std::string& ApiServer, const std::string& LoginURL)
{
	//checking if this method was already called.
	if (m_AppKey != "")
	{
		m_ErrorDescription = "FacebookAsyns::Init(). Method was already called! It must be called only once.";
		return false;
	}

	//storing passed params
	m_AppKey = AppKey;
	m_AppSecret = AppSecret;
	m_ApiServer = ApiServer;
	m_LoginURL = LoginURL;

	//TODO - to make customizable
	m_SecureApiServer = "https://api.facebook.com/restserver.php";

	//validating params
	if (m_AppKey.size() < 1 || m_AppSecret.size() < 1 || m_ApiServer.size() < 1 || m_LoginURL.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::Init(). Invalid params were passed!";
		return false;
	}

	//returning success
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::Init(const std::string& AppKey, const std::string& AppSecret, const std::string& ApiServer, const std::string& LoginURL)




//Working with users
//==========================================================================

bool FacebookAsync::SetCurrentUser(const std::string& UserEmail, const std::string& UserPassword)
{
	//checking if init wasn't called
	if (m_AppKey.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::SetCurrentUser(). Facebook::Init() wasn't called!";
		return false;
	}


	//validating params
	if (UserEmail.size() < 1 || UserPassword.size() < 1)
	{
		m_ErrorDescription = "FacebookAsync::SetCurrentUser(). Invalid params passed!";
		return false;
	}

	//resetting current user
	m_pCurUser = NULL;

	bool MustAddUser = true;

	if (m_UsersList.empty() == false)
	{
		//checking if user is already in the list
		std::list<FacebookAsyncUser*>::iterator Iter;

		for (Iter = m_UsersList.begin(); Iter != m_UsersList.end(); Iter++)
		{
			if ((*Iter) == NULL)
			{
				m_ErrorDescription = "Facebook::SetCurrentUser(). Internal error: NULL pointer in users list!";
				return false;
			}

			if ((*Iter)->m_UserEmail == UserEmail)
			{
				//user found.
				m_pCurUser = (*Iter);

				//checking for weird error
				//if (m_pCurUser->m_LoggedIn == true && m_pCurUser->m_UserPassword != UserPassword)
				//{
				//	m_ErrorDescription = "Facebook::SetCurrentUser(). User already logged in with different password! Weird!";
				//	return false;
				//}

				//remembering the new password
				m_pCurUser->m_UserPassword = UserPassword;

				MustAddUser = false;
				break;
			}
		}
	}

	//creating new user if needed
	if (MustAddUser == true)
	{
		m_pCurUser = new FacebookAsyncUser(UserEmail, UserPassword);

		//putting current user in the list
		m_UsersList.push_back(m_pCurUser);
	}


	//success
	m_ErrorDescription = "";
	return true;


}//bool FacebookAsync::SetCurrentUser(const std::string& UserEmail, const std::string& UserPassword)



bool FacebookAsync::RemoveCurrentUser()
{
	if (m_pCurUser == NULL)
	{
		m_ErrorDescription = "FacebookAsync::RemoveCurrentUser(). Current user was not set!";
		return false;
	}

	//if current user was logged in - making log out.
	//if (m_pCurUser->m_LoggedIn == true)
	//	Helper_LogoutUser(m_pCurUser);

	//removing user from users list
	std::list<FacebookAsyncUser*>::iterator Iter;
	bool UserWasFound = false;

	for (Iter = m_UsersList.begin(); Iter != m_UsersList.end(); Iter++)
	{
		if ((*Iter) == m_pCurUser)
		{
			m_UsersList.erase(Iter);
			UserWasFound = true;
			break;
		}
	}

	if (UserWasFound == false)
	{
		m_ErrorDescription = "FacebookAsync::RemoveCurrentUser(). Internal error: Cur user wasn't found in users list!";
		return false;
	}

	//deletting cur cur user
	delete m_pCurUser;
	m_pCurUser = NULL;

	//success
	m_ErrorDescription = "";
	return true;

}//bool FacebookAsync::RemoveCurrentUser()



//Updating current request(s)
//==========================================================================

void FacebookAsync::Update(float dt)
{

	//updating current user
	if (m_pCurUser != NULL)
		m_pCurUser->Update(dt);

}


void FacebookAsync::MakeCallback()
{
	if (m_pCallbackFunc != NULL)
		m_pCallbackFunc();
}